package demon1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Callable;

class Student{
    //私有属性name
    private String name = "bit";
    //公有属性age
    public int age = 18;
    //不带参数的构造方法
    public Student(){
        System.out.println("Student()");
    }
    private Student(String name,int age) {
        this.name = name;
        this.age = age;
        System.out.println("Student(String,name)");
    }
    private void eat(){
        System.out.println("i am eat");
    }
    public void sleep(){
        System.out.println("i am pig");
    }
    private void function(String str) {
        System.out.println(str);
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    /**
     * 得出结论
     * Class 对象只有一个
     * @param args
     */
    public static void main1(String[] args) {
        //这种方法重点掌握
        Class<?> c1=null;
        try {
           c1= Class.forName("demon1.Student");
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }

        //System.out.println(c1);C
        Class <?> c2=Student.class;

        Student student=new Student();
        Class <?> c3=student.getClass();  //会调用构造方法
        System.out.println(c1==c2);
    }

    public static void main2(String[] args) {
        Class<?> c1=null;
        try{
            c1=Class.forName("demon1.Student");
            Student student=(Student)c1.newInstance();
            System.out.println(student);
        }catch(ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (InstantiationException e){
            throw new RuntimeException(e);
        }catch(IllegalAccessException e){
            throw new RuntimeException(e);
        }
    }

    public static void main3(String[] args) {
        Class<?>c1=null;
        try{
            c1=Class.forName("demon1.Student");
            Constructor<Student>constructor=
                    (Constructor<Student>)c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);

            Student student=constructor.newInstance("caocao",17);
            System.out.println(student);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (NoSuchMethodException e){
            throw new RuntimeException(e);
        }catch (SecurityException e){
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main4(String[] args) {
        Class<?> c1=null;
        try{
            c1=Class.forName("demon1.Student");
            Constructor<Student>constructor=
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student=constructor.newInstance("caocao",13);
            System.out.println(student);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main5(String[] args) {
        Class<?> c1=null;
        try{
            c1=Class.forName("demon1.Student");
            Constructor<Student>constructor=(Constructor<Student>)c1.getDeclaredConstructor(String.class,int.class);
           constructor.setAccessible(true);
           Student student=constructor.newInstance("caocao",13);
        }catch (ClassNotFoundException | NoSuchMethodException e){
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main6(String[] args) {
        try{
            Class<?> c1=Class.forName("demon1.Student");
            Constructor<?> constructor =
                    (Constructor<?>)c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("gaomemgbo",13);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (NoSuchMethodException e){
            throw new RuntimeException(e);
        }catch (SecurityException e){
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void main7(String[] args) {
        try{
            Class<?> c1=Class.forName("demon1.Student");
            Constructor<?> constructor =
                    (Constructor<?>)c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("gaomemgbo",13);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (NoSuchMethodException e){
            throw new RuntimeException(e);
        }catch (SecurityException e){
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Class<?> c1=Class.forName("demon1.Student");
            Field field= c1.getDeclaredField("name");
            field.setAccessible(true);
            Object obj=c1.newInstance();
            Student student=(Student) obj;
            field.set("xiaoming",19);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}
